package com.tarena.day050;

import java.util.Arrays;

/**0 1 2 34 5 6 7 
 * 统计一个字符在字符数组中的所有位置. */
public class CountAllDemo {
  public static void main(String[] args) {
    String str = "统计一个字符在字符数组中的所有位置";
    int[] ary = count(str, '字');
    System.out.println(Arrays.toString(ary)); 
  }
  public static int[] count(String str, char key){
    int[] ary = {};//new int[0]
    for(int i=0; i<str.length(); i++){
      char c = str.charAt(i);
      //跟踪语句//输出当前的变量状态值
      if(c==key){
        //i->{};
        //跟踪语句
        ary = Arrays.copyOf(ary, ary.length+1);
        ary[ary.length-1]=i;
      }
    }
    return ary;
  }
}
